package com.oocl.task0724.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oocl.task0724.dao.UserDao;
import com.oocl.task0724.dao.impl.UserDaoImpl;
import com.oocl.task0724.pojo.User;

/**
 * Servlet implementation class UserListServlet
 */
public class UserListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private UserDao userdao=new UserDaoImpl();
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserListServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println( "--------------" );
		
		List<User> userList = userdao.getAll();
		
		StringBuilder buf=new StringBuilder();
		
		String htmlPart1="<table><tr><th>username</th><th>salary</th><th>birth</th><th>sex</th><th>headImg</th></tr>";
		buf.append( htmlPart1 );
		
		for (User user : userList) {
			buf.append("<tr>");
				
				buf.append("<td>");
				buf.append(user.getUsername());
				buf.append("</td>");
				
				buf.append("<td>");
				buf.append(user.getSalary());
				buf.append("</td>");
				
				buf.append("<td>");
				buf.append( new SimpleDateFormat("yyyy-MM-dd").format( user.getBirth() ) );
				buf.append("</td>");
				
				buf.append("<td>");
				buf.append(user.getIsMale() ? "male" : "female");
				buf.append("</td>");
				
				
				String imgSrc="/homework0724/UserImageServlet?id=" + user.getId();
				buf.append("<td>");
//				buf.append("<img src='" + imgSrc + "' />");
				buf.append("<iframe src=\""+ imgSrc +"\"></iframe>");
				buf.append("</td>");
				
			buf.append("</tr>");
		}
		buf.append("</table>");

		
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write( buf.toString() );
	
		
	}

}
